<?php
// 如果需要处理服务器端逻辑，可以在这里添加
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            flex-direction: column;
        }
        .login-container {
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 350px;
            padding: 30px;
            margin-bottom: 20px;
        }
        h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 4px;
            width: 100%;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #2980b9;
        }
        button:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
        }
        .message {
            border-radius: 4px;
            padding: 10px;
            margin-bottom: 20px;
            text-align: center;
        }
        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .success-message {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .register-link {
            text-align: center;
            margin-top: 20px;
            font-size: 14px;
        }
        .register-link a {
            color: #3498db;
            text-decoration: none;
        }
        .register-link a:hover {
            text-decoration: underline;
        }
        .json-response {
            background-color: white;
            border-radius: 5px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 350px;
            padding: 20px;
            margin-top: 20px;
        }
        .json-response h3 {
            margin-top: 0;
            color: #333;
        }
        .json-content {
            background-color: #f8f9fa;
            border: 1px solid #e9ecef;
            border-radius: 4px;
            padding: 15px;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            white-space: pre-wrap;
            word-wrap: break-word;
            max-height: 300px;
            overflow-y: auto;
        }
        .loading {
            text-align: center;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>用户登录</h2>
        
        <div id="message" class="message" style="display: none;"></div>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required>
            </div>
            
            <button type="submit" id="loginBtn">登录</button>
            
            <div class="register-link">
                还没有账号？<a href="register.php">立即注册</a>
            </div>
        </form>
    </div>

    <div id="jsonResponse" class="json-response" style="display: none;">
        <h3>服务器响应</h3>
        <div id="jsonContent" class="json-content"></div>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault(); // 阻止表单默认提交
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const loginBtn = document.getElementById('loginBtn');
            const messageDiv = document.getElementById('message');
            const jsonResponseDiv = document.getElementById('jsonResponse');
            const jsonContentDiv = document.getElementById('jsonContent');
            
            // 显示加载状态
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';
            messageDiv.style.display = 'none';
            jsonResponseDiv.style.display = 'none';
            
            // 准备要发送的数据
            const formData = new FormData();
            formData.append('username', username);
            formData.append('password', password);
            
            // 发送AJAX请求
            fetch('user/login', {
                method: 'POST',
                body: formData
            })
            .then(response => {
                // 获取响应文本
                return response.text().then(text => {
                    // 尝试解析JSON
                    let jsonData;
                    try {
                        jsonData = JSON.parse(text);
                    } catch (e) {
                        jsonData = { error: '服务器返回了无效的JSON格式', raw_response: text };
                    }
                    
                    return {
                        status: response.status,
                        ok: response.code,
                        data: jsonData,
                        rawText: text
                    };
                });
            })
            .then(result => {
                // 显示JSON响应
                jsonContentDiv.textContent = JSON.stringify(result.data, null, 2);
                jsonResponseDiv.style.display = 'block';
                
                // 显示消息
                messageDiv.style.display = 'block';
                if (result.data.code == 200 ) {
                    messageDiv.className = 'message success-message';
                    messageDiv.textContent = result.data.message || '欢迎您'+result.data.data.userInfo.nickname;
                    
                    // 如果登录成功，可以在这里添加跳转逻辑
                    // setTimeout(() => {
                    //     window.location.href = 'dashboard.php';
                    // }, 2000);
                } else {
                    messageDiv.className = 'message error-message';
                    messageDiv.textContent = result.data.message || result.data.error || '登录失败，请检查用户名和密码';
                }
            })
            .catch(error => {
                console.error('请求错误:', error);
                
                // 显示错误信息
                messageDiv.style.display = 'block';
                messageDiv.className = 'message error-message';
                messageDiv.textContent = '网络错误或服务器无响应';
                
                // 显示错误的JSON响应
                jsonContentDiv.textContent = JSON.stringify({
                    error: '网络请求失败',
                    details: error.message
                }, null, 2);
                jsonResponseDiv.style.display = 'block';
            })
            .finally(() => {
                // 恢复按钮状态
                loginBtn.disabled = false;
                loginBtn.textContent = '登录';
            });
        });
    </script>
</body>
</html>
